package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Spot;
import cn.wolfcode.wolf2w.query.SpotQuery;
import cn.wolfcode.wolf2w.repository.SpotRepository;
import cn.wolfcode.wolf2w.service.ISpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
//@Transacyional mongodb事务必须复制集方式
public class SpotServiceImpl implements ISpotService {

    @Autowired
    private SpotRepository spotRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Spot spot) {
        spotRepository.save(spot);
    }

    @Override
    public void update(Spot spot) {
        spotRepository.save(spot);
    }

    @Override
    public void delete(String id) {
        spotRepository.deleteById(id);
    }

    @Override
    public Spot get(String id) {
        return spotRepository.findById(id).orElse(null);
    }

    @Override
    public List<Spot> list() {
        return spotRepository.findAll();
    }


    @Override
    public void saveOrUpdate(Spot spot) {
        if (StringUtils.hasLength(spot.getId())){
            //特殊案例 页面更新参数和集合中个数一致 相当一全量更新
            this.update(spot);
        }else{
            this.save(spot);
        }
    }

}